﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Table = FCNS.Data.Table;

namespace DoubleH.Utility
{
    /// <summary>
    /// WindowAlert.xaml 的交互逻辑
    /// </summary>
    public partial class WindowAlert : Window
    {
        Table.SysConfig sysConfig = Table.SysConfig.SysConfigParams;
        Table.UserS.UserAuthority authority = Table.UserS.LoginUser.Authority;

        public WindowAlert()
        {
            InitializeComponent();

            InitVar();
        }

        private void InitVar()
        {
            //if (sysConfig.UseAfterSaleServiceS || !Table.UserS.LoginUser.Authority.AfterSaleServiceUsed)
            //{
            //    dataGridObjectAfterSaleService.LoadingRow += (ss, ee) => { ee.Row.Header = ee.Row.GetIndex() + 1; };
            //    dataGridObjectWeiBao.LoadingRow += (ss, ee) => { ee.Row.Header = ee.Row.GetIndex() + 1; };
            //}
            if (sysConfig.UseStoreS)
            {
                dataGridObjectChuKu.InitColumns(DataTableText.仓库单据出库单);
                dataGridObjectRuKu.InitColumns(DataTableText.仓库单据入库单);
            }
            if (sysConfig.UsePayS)
            {
                dataGridObjectShouKuan.InitColumns(DataTableText.收款单);
                dataGridObjectFuKuan.InitColumns(DataTableText.付款单);
            }
        }

        public bool Init()
        {
            InitStoreIO();
            InitAfterSaleServiceS();
            InitMoney();

            return ConfigUI();
        }

        private void InitStoreIO()
        {
            if (!sysConfig.UseStoreS||authority.StoreUsed)
                return;

            Table.StoreOrderS order = new Table.StoreOrderS(Table.StoreOrderS.EnumFlag.出入库单);
            if (authority.StoreChuKu)
                dataGridObjectChuKu.ItemsSource = order.GetList(true, true);

            if (authority.StoreRuKu)
                dataGridObjectRuKu.ItemsSource = order.GetList(false, true);
        }

        private void InitAfterSaleServiceS()
        {
            if (!sysConfig.UseAfterSaleServiceS||!authority.AfterSaleServiceUsed)
                return;

            #region 超期售后
            dataGridObjectAfterSaleService.Items.Clear();
            double hour;
            foreach (Table.AfterSaleServiceS ass in Table.AfterSaleServiceS.GetList(null,null,null,Table.AfterSaleServiceS.EnumEnable.客户报修))
            {
                //检测维保用户
                Table.UniqueS us = Table.WeiBaoS.WeiBaoFlag(ass.CorSId);
                if (us != null)
                {
                    hour = us.DoubleValue;
                    ass._WeiBaoFlag = us.Name;
                }
                else
                    hour = sysConfig.RequestTime;

                if (ass.BaoXiuDateTime.AddHours(hour) < DateTime.Now)
                    dataGridObjectAfterSaleService.Items.Add(ass);
            }
            #endregion

            #region 维保合同
            if (sysConfig.WeiBaoAlertDay != 0)
            {
                dataGridObjectWeiBao.Items.Clear();
                foreach (Table.WeiBaoS wbs in Table.WeiBaoS.GetListWhereWillEnd(Table.WeiBaoS.EnumFlag.外包维保, sysConfig.WeiBaoAlertDay))
                    dataGridObjectWeiBao.Items.Add(wbs);
            }
            #endregion
        }

        private void InitMoney()
        {
            if (!sysConfig.UsePayS||!authority.MoneyUsed)
                return;

            Table.PayS order = new Table.PayS();
            dataGridObjectFuKuan.ItemsSource = order.GetList(true, true);
            dataGridObjectShouKuan.ItemsSource = order.GetList(false, true);
        }

        private bool ConfigUI()
        {
            tabItemChuKu.Visibility = dataGridObjectChuKu.HasItems ? Visibility.Visible : Visibility.Collapsed;
            tabItemRuKu.Visibility = dataGridObjectRuKu.HasItems ? Visibility.Visible : Visibility.Collapsed;

            tabItemAfterSaleService.Visibility = dataGridObjectAfterSaleService.HasItems ? Visibility.Visible : Visibility.Collapsed;
            tabItemWeiBao.Visibility = dataGridObjectWeiBao.HasItems ? Visibility.Visible : Visibility.Collapsed;

            tabItemShouKuan.Visibility = dataGridObjectShouKuan.HasItems ? Visibility.Visible : Visibility.Collapsed;
            tabItemFuKuan.Visibility = dataGridObjectFuKuan.HasItems ? Visibility.Visible : Visibility.Collapsed;

            bool have = false;
            foreach (TabItem ti in tabControl1.Items)
                if (ti.Visibility == Visibility.Visible)
                {
                    tabControl1.SelectedItem = ti;
                    have = true;
                    break;
                }
            return have;
        }

        //超库存上限商品
        //低于库存下限商品
        //促销到期报警
    }
}
